﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Snake.Net;

namespace Snake
{
    public partial class Frm_Register : Form
    {
        public Frm_Register()
        {
            InitializeComponent();
        }

        private void btn_regist_Click(object sender, EventArgs e)
        {
            if(tbx_acount.Text.Length<5)
            {
                lbl_inf_acount.Text = "用户名太短！";
            }
            else if(tbx_acount.Text.Length > 12)
            {
                lbl_inf_acount.Text = "用户名过长，\n不能超过12位！";
            }
            else if(tbx_password.Text.Length<6)
            {
                lbl_inf_password.Text = "密码过短!";
            }
            else if(tbx_password.Text.Length>16)
            {
                lbl_inf_password.Text = "密码过长，\n不能超过16位！";
            }
            else if(tbx_password.Text!=tbx_makesure.Text)
            {
                lbl_inf_makesure.Text = "两次输入的密码不一致！\n请重新输入！";
            }
            else
            {
                //用户名密码上传到服务器
                string result;
                result = HttpRequest.Regist( tbx_acount.Text, tbx_password.Text);
                if (result == "OK")
                {
                    MessageBox.Show("注册成功！");
                    Close();
                }
                else
                {
                    MessageBox.Show(result);
                }
            }
        }

        private void tbx_acount_TextChanged(object sender, EventArgs e)
        {
            lbl_inf_makesure.Text = "";
            lbl_inf_acount.Text = "";
            lbl_inf_password.Text = "";
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Frm_Register_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        protected override void OnShown(EventArgs e)
        {
            Graphics g = CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.White, 3f), 0, 0, Width - 3, Height - 3);
        }
    }
}
